package com.erp.order.purchasing.controller;

import com.erp.hr.base.controller.BaseController;
import com.erp.hr.base.model.ApprovalVO;
import com.erp.hr.base.vo.ResultVO;
import com.erp.order.purchasing.model.InquiryList;
import com.erp.order.purchasing.model.PurchasingAudit;
import com.erp.order.purchasing.service.InquiryListService;
import com.erp.order.purchasing.service.PurchasingAuditService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 审批 控制层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Controller
@RequestMapping("web/approval")
public class ApprovalController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ApprovalController.class);

    /**
     * 采购审核服务
     */
    @Autowired
    private PurchasingAuditService purchasingAuditService;

    /**
     * 询价单服务
     */
    @Autowired
    private InquiryListService inquiryListService;


    /**
     * 跳转至采购计划审批页
     * @param purchasingAudit 采购计划对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("toPurchasingApproval")
    public String toPurchasingApproval(PurchasingAudit purchasingAudit, Model model) {
        model.addAttribute("purchasingAudit",
                purchasingAuditService.addCompleteData(
                        purchasingAuditService.getDataObject(
                                purchasingAudit.getCode())));
        model.addAttribute("taskId", purchasingAudit.getId());
        model.addAttribute("taskInfoList", purchasingAuditService.getTaskInfo(purchasingAudit.getProcessInstanceId()));
        model.addAttribute("typeMap", purchasingAuditService.getDictByKey("audit_type"));
        model.addAttribute("subTypeMap", purchasingAuditService.getDictByKey("requisition_type"));
        model.addAttribute("statusMap", purchasingAuditService.getDictByKey("requisition_status"));
        if (purchasingAuditService.isAssignNext(purchasingAudit.getProcessInstanceId())) {
            model.addAttribute("nextList", purchasingAuditService.getBuyer(purchasingAudit.getCode()));
            return "basic.jsp?content=approval/purchasingApprovalAssign";
        } else {
            return "basic.jsp?content=approval/purchasingApproval";
        }
    }

    /**
     * 执行采购计划审批
     * @param approvalVO 审批vo对象
     * @return 审批结果
     */
    @ResponseBody
    @RequestMapping("doPurchasingApproval")
    public ResultVO<String> doPurchasingApproval(@RequestBody ApprovalVO approvalVO) {
        return purchasingAuditService.approval(approvalVO);
    }

    /**
     * 跳转至比价审批页
     * @param inquiryList 询价单对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("toPriceParityApproval")
    public String toPriceParityApproval(InquiryList inquiryList, Model model) {
        model.addAttribute("inquiryList",
                inquiryListService.addCompleteData(
                        inquiryListService.getByCode(
                                inquiryList.getCode())));
        model.addAttribute("taskId", inquiryList.getId());
        model.addAttribute("taskInfoList", inquiryListService.getTaskInfo(inquiryList.getProcessInstanceId()));
        model.addAttribute("procurementTypeMap", inquiryListService.getDictByKey("procurement_type"));
        model.addAttribute("inquiryTypeMap", inquiryListService.getDictByKey("inquiry_type"));
        model.addAttribute("quoteRequestMap", inquiryListService.getDictByKey("quote_request"));
        model.addAttribute("tradingMannerMap", inquiryListService.getDictByKey("trading_manner"));
        model.addAttribute("designatedSettlementDateMap", inquiryListService.getDictByKey("designated_settlement_date"));
        model.addAttribute("settlementDateMap", inquiryListService.getDictByKey("settlement_date"));
        model.addAttribute("paymentStageMap", inquiryListService.getDictByKey("payment_stage"));
        model.addAttribute("paymentMap", inquiryListService.getDictByKey("payment"));
        model.addAttribute("invoiceRequirementsMap", inquiryListService.getDictByKey("invoice_requirements"));
        model.addAttribute("certificationRequirementsMap", inquiryListService.getDictByKey("certification_requirements"));
        model.addAttribute("bookBuildingMap", inquiryListService.getDictByKey("book_building"));
        model.addAttribute("procurementPortalMap", inquiryListService.getDictByKey("procurement_portal"));
        model.addAttribute("priceComparisonWayMap", inquiryListService.getDictByKey("price_comparison_way"));
        model.addAttribute("contactWayMap", inquiryListService.getDictByKey("contact_way"));
        model.addAttribute("procurementMethodMap", inquiryListService.getDictByKey("procurement_method"));
        model.addAttribute("unitMap", inquiryListService.getDictByKey("material_unit"));
        return "basic.jsp?content=approval/priceParityApproval";
    }

    /**
     * 执行比价审批
     * @param approvalVO 审批vo对象
     * @return 审批结果
     */
    @ResponseBody
    @RequestMapping("doPriceParityApproval")
    public ResultVO<String> doPriceParityApproval(@RequestBody ApprovalVO approvalVO) {
        return inquiryListService.approval(approvalVO);
    }

}